package cn.com.twoke.application.game.mario.game.system;

import cn.com.twoke.application.game.mario.core.ecs.component.PositionComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.RigidbodyComponent;
import cn.com.twoke.application.game.mario.core.ecs.entity.Entity;
import cn.com.twoke.application.game.mario.core.ecs.system.LogicGameSystem;
import cn.com.twoke.application.game.mario.game.component.AutoMoveComponent;

import java.util.List;

/**
 * 敌人游戏对象系统
 */
public class AutoMovingSystem implements LogicGameSystem {


    @Override
    public void update(List<Entity> entities, double delta) {
        for (Entity entity : entities) {
            if (entity.hasComponent(PositionComponent.class) &&
                    entity.hasComponent(AutoMoveComponent.class) && entity.hasComponent(RigidbodyComponent.class)) {
                doUpdateNeedAutoMovingEntity(entity, delta);
            }
        }
    }

    /**
     * 更新需要自动移动的游戏实体
     *
     * @param entity 游戏对象
     * @param delta  时间间隔
     */
    private void doUpdateNeedAutoMovingEntity(Entity entity, double delta) {
        RigidbodyComponent rigidbodyComponent = entity.getComponent(RigidbodyComponent.class);
        entity.getComponent(AutoMoveComponent.class).doAutoMove(rigidbodyComponent);
    }

}
